import {
  getTimestamp,
  filterTimeHMS
} from './time'

const Order = class Order {
  constructor(props) {
    this.realname = props.realname,
    this.mobile = props.mobile,
    this.applyTime = props.applyTime,
    this.status = props.status,
    this.payTime = props.payTime,
    this.lastRepayDay = props.lastRepayDay,
    this.repayTime = props.repayTime,
    this.leftRepayMoneyStart = props.leftRepayMoneyStart,
    this.leftRepayMoneyEnd = props.leftRepayMoneyEnd,
    this.overdueTag = props.overdueTag,
    this.size = props.size,
    this.page = props.page
  }

  getState() {
    let x = {}
    //姓名
    if (this.realname) {
      x.realname = this.realname
    }

    //下单时间
    if (this.applyTime) {
      x.applyStartTime = this.applyTime ? getTimestamp(this.applyTime[0]) : undefined;
      x.applyEndTime = this.applyTime ? getTimestamp(this.applyTime[1]) + 86399999 : undefined;
    }
    //状态
    if (this.status) {
      x.status = this.status
    }
    //放款时间
    if (this.payTime) {
      x.payStartTime = this.payTime ? getTimestamp(this.payTime[0]) : undefined;
      x.payEndTime = this.payTime ? getTimestamp(this.payTime[1]) + 86399999 : undefined;
    }
    //到期时间
    if (this.lastRepayDay) {
      x.lastRepayDayStart = this.lastRepayDay ? filterTimeHMS(getTimestamp(this.lastRepayDay[0])) : undefined;
      x.lastRepayDayEnd = this.lastRepayDay ? filterTimeHMS(getTimestamp(this.lastRepayDay[1])) : undefined;
    }
    //结清时间
    if (this.repayTime) {
      x.repayStartTime = this.repayTime ? getTimestamp(this.repayTime[0]) : undefined;
      x.repayEndTime = this.repayTime ? getTimestamp(this.repayTime[1]) + 86399999 : undefined;
    }

    //有无逾期
    if (this.overdueTag) {
      x.overdueTag = this.overdueTag
    }
    x.size = this.size;
    x.page = this.page;

    if (!this.mobile.isEmpty()) {
      if (this.validateMobile(this.mobile)) {
        x.mobile = this.mobile
      } else {
        return {"isValid": false, "message": "请输入正确的手机号"};
      }
    }

    if (!this.leftRepayMoneyStart.isEmpty() || !this.leftRepayMoneyEnd.isEmpty()) {
      if (this.leftRepayMoneyStart.isEmpty()) {
        if (this.validateInt(this.leftRepayMoneyEnd)) {
          x.leftRepayMoneyEnd = this.leftRepayMoneyEnd
        } else {
          return {"isValid": false, "message": "请输入整数金额"};
        }
      } else if (this.leftRepayMoneyEnd.isEmpty()) {
        if (this.validateInt(this.leftRepayMoneyStart)) {
          x.leftRepayMoneyStart = this.leftRepayMoneyStart
        } else {
          return {"isValid": false, "message": "请输入整数金额"};
        }
      } else {
        if (Number(this.leftRepayMoneyEnd) >= Number(this.leftRepayMoneyStart)) {
          x.leftRepayMoneyStart = this.leftRepayMoneyStart
          x.leftRepayMoneyEnd = this.leftRepayMoneyEnd
        } else {
          return {"isValid": false, "message": "请设置起始金额不要超过结束金额"};
        }
      }
    }

    return x
  }

  validateInt(value) {
    return !value.isEmpty() && /^[0-9]*$/.test(value);
  }

  validateMobile(value) {
    return !value.isEmpty() && /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(value);
  }
}

export default Order
